package javaday04Maze;

import java.util.Random;
import java.util.Scanner;

public class Demo {
    static Role person = new Role(1, "用户", 100, 10);
    static Weapon wp1 = new Weapon("无尽之刃", 20);
    static Weapon wp2 = new Weapon("破晓", 25);
    static Armour ar1 = new Armour("不祥征兆", 50);
    static Monster m1 = new Monster("狼", 60, 10);
    static Fashu f1 = new Fashu("天雷滚滚", 50);
    static Medicine medicine = new Medicine("治疗药水", 30); // 定义治疗药水

    public static void main(String args[]) {
        System.out.println("欢迎来到迷宫游戏，0为可移动的地区，|为墙壁,P为玩家,M为怪物,W为武器,A为装备,H为药水");
        System.out.println("您当前的状态为: 生命值: " + person.getBlood() + "\n攻击力：" + person.getHurt());
        System.out.println("输入wasd进行移动, 输入zhuangtai进行角色生命值以及攻击力的查询");
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        int[][] migong = new int[8][8];
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                migong[i][j] = random.nextInt(10);
            }
        }
        // 初始位置设置成用户
        migong[0][0] = 10;
        printMigong(migong);
        leaveMigong(migong, person);
    }

    public static void printMigong(int[][] migong) {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (migong[i][j] == 10) {
                    System.out.print("P" + " ");
                    continue;
                }
                if (migong[i][j] == 1 || migong[i][j] == 2) {
                    System.out.print("|" + " ");
                    continue;
                } else if (migong[i][j] == 3 || migong[i][j] == 4 || migong[i][j] == 5) {
                    System.out.print("M" + " ");
                    continue;
                } else if (migong[i][j] == 6) {
                    System.out.print("W" + " ");
                    continue;
                } else if (migong[i][j] == 7) {
                    System.out.print("A" + " ");
                    continue;
                } else if (migong[i][j] == 8) {
                    System.out.print("H" + " "); // 药水
                    continue;
                } else {
                    System.out.print("0" + " ");
                }
            }
            System.out.println();
        }
    }

    public static void leaveMigong(int[][] migong, Role person) {
        int lie = 0;
        int hang = 0;
        Scanner scanner = new Scanner(System.in);
        while (hang != 7 || lie != 7) {
            if (person.getBlood() <= 0) { // 检查用户是否死亡
                System.out.println("您死亡了，游戏结束");
                return; // 结束游戏
            }
            String option = scanner.next();
            switch (option) {
                case "s":
                    if (hang + 1 < 8 && migong[hang + 1][lie] != 1 && migong[hang + 1][lie] != 2) {
                        handleTile(migong, person, hang, lie, hang + 1, lie);
                        hang++;
                    } else {
                        System.out.println("不可通行");
                    }
                    break;
                case "w":
                    if (hang - 1 >= 0 && migong[hang - 1][lie] != 1 && migong[hang - 1][lie] != 2) {
                        handleTile(migong, person, hang, lie, hang - 1, lie);
                        hang--;
                    } else {
                        System.out.println("不可通行");
                    }
                    break;
                case "a":
                    if (lie - 1 >= 0 && migong[hang][lie - 1] != 1 && migong[hang][lie - 1] != 2) {
                        handleTile(migong, person, hang, lie, hang, lie - 1);
                        lie--;
                    } else {
                        System.out.println("不可通行");
                    }
                    break;
                case "d":
                    if (lie + 1 < 8 && migong[hang][lie + 1] != 1 && migong[hang][lie + 1] != 2) {
                        handleTile(migong, person, hang, lie, hang, lie + 1);
                        lie++;
                    } else {
                        System.out.println("不可通行");
                    }
                    break;
                case "zhuangtai":
                    System.out.println("您当前的状态为: 生命值: " + person.getBlood() + " 攻击力：" + person.getHurt());
                    break;
            }
            printMigong(migong);
        }
        if (hang == 7 && lie == 7) {
            System.out.println("恭喜你成功通关");
        }
    }

    public static void handleTile(int[][] migong, Role person, int oldHang, int oldLie, int newHang, int newLie) {
        int tile = migong[newHang][newLie];
        migong[oldHang][oldLie] = 0; // 玩家离开原位置
        switch (tile) {
            case 0:
                migong[newHang][newLie] = 10; // 玩家移动到新位置
                break;
            case 6: // 武器
                person.wearWeapon(wp1);
                System.out.println("您当前的状态为: 生命值: " + person.getBlood() + " 攻击力：" + person.getHurt());
                migong[newHang][newLie] = 10;
                break;
            case 7: // 盔甲
                person.wearArmour(ar1);
                System.out.println("您当前的状态为: 生命值: " + person.getBlood() + " 攻击力：" + person.getHurt());
                migong[newHang][newLie] = 10;
                break;
            case 8: // 药水
                person.addMedicine(medicine);
                migong[newHang][newLie] = 10;
                break;
            case 3:
            case 4:
            case 5: // 怪物
                battle(person);
                migong[newHang][newLie] = 10;
                break;
        }
    }

    public static void battle(Role person) {
        int count = 0;
        System.out.println("您遭遇了怪物，1为选择攻击，2为选择法术攻击,3可以选择喝下治疗药水");
        System.out.println("您当前的状态为: 生命值: " + person.getBlood() + " 攻击力：" + person.getHurt());
        System.out.println("怪物的状态为：生命值：" + m1.getBlood() + " 攻击力：" + m1.getHurt());
        while (true) {
            Scanner now = new Scanner(System.in);
            int actionPosition = now.nextInt();
            switch (actionPosition) {
                case 1:
                    Random r = new Random();
                    int hurtMan = r.nextInt(person.getHurt()) + 10;
                    int hurtMonster = r.nextInt(m1.getHurt()) + 7;
                    System.out.println("您对怪物造成了 " + hurtMan + " 点伤害");
                    System.out.println("怪物对您造成了 " + hurtMonster + " 点伤害");
                    person.setBlood(person.getBlood() - hurtMonster);
                    m1.setBlood(m1.getBlood() - hurtMan);
                    System.out.println("您当前的状态为: 生命值: " + person.getBlood() + " 攻击力：" + person.getHurt());
                    System.out.println("怪物的状态为：生命值：" + m1.getBlood() + " 攻击力：" + m1.getHurt());
                    break;
                case 2:
                    if (count == 1) {
                        System.out.println("每次战斗法术技能只能使用一次");
                        break;
                    }
                    Random r2 = new Random();
                    int hurtMan2 = r2.nextInt(f1.getHurt()) + 15;
                    int hurtMonster2 = r2.nextInt(m1.getHurt()) + 7;
                    System.out.println("您使用了 " + f1.getName() + ", 您对怪物造成了 " + hurtMan2 + " 点伤害");
                    System.out.println("怪物对您造成了 " + hurtMonster2 + " 点伤害");
                    person.setBlood(person.getBlood() - hurtMonster2);
                    m1.setBlood(m1.getBlood() - hurtMan2);
                    System.out.println("您当前的状态为: 生命值: " + person.getBlood() + " 攻击力：" + person.getHurt());
                    System.out.println("怪物的状态为：生命值：" + m1.getBlood() + " 攻击力：" + m1.getHurt());
                    count++;
                    break;
                case 3:
                    person.useMedicine(); // 使用药水
                    break;
            }
            if (person.getBlood() <= 0) {
                System.out.println("您死亡了");
                return; // 结束战斗
            } else if (m1.getBlood() <= 0) {
                System.out.println("您战胜了怪物");
                m1.setBlood(60); // 重置怪物血量
                return; // 结束战斗
            }
            System.out.println("您遭遇了怪物，1为选择攻击，2为选择法术攻击,3可以选择喝下治疗药水");
        }
    }
}